/*
 * 英才汇硕信息科技有限公司 拥有本软件版权 2021 并保留所有权利。
 * Copyright 2021, YCHS Information&Science Techology Co.,Ltd,
 * All right reserved.
 */
package com.ychs.lawyerback.controller;

import com.ychs.lawyerback.annotation.RequiredPermission;
import com.ychs.lawyerback.common.R;
import com.ychs.lawyerback.dto.UserOptDTO;
import com.ychs.lawyerback.dto.UserQueryDTO;
import com.ychs.lawyerback.dto.UsernameCheckDTO;
import com.ychs.lawyerback.mapper.RoleMapper;
import com.ychs.lawyerback.service.UserService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.relation.Role;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mayujing
 * @version 1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RoleMapper roleMapper;


    @RequestMapping("/getUsersByCondition")
    @RequiredPermission("system:user:query")
    public R<?> getUsersByCondition(UserQueryDTO userQueryDTO) {
        Map<String,Object> map = userService.getUsersByCondition(userQueryDTO);
        return R.success(map);
        //这里除了数据还有total里的数据,所以用map封装来传值
    }

    @PostMapping("/addUser")
    @RequiredPermission("system:user:add")
    public R<?> addUser(@Valid @RequestBody UserOptDTO userOptDTO) {
        long res = userService.addUser(userOptDTO);
        return res>0?R.success():R.fail();
    }
    @PutMapping("/editUser")
    @RequiredPermission("system:user:edit")
    public R<?> editUser(@Valid @RequestBody UserOptDTO userOptDTO) {
        int res = userService.editUser(userOptDTO);
        return res>0?R.success():R.fail();
    }

    @DeleteMapping("/deleteUser")
    public R<?> deleteUser(@RequestBody Map<String,List<Long>> map) {
        int res = userService.deleteUser(map.get("ids"));
        return res > 0 ? R.success() : R.fail();
    }

//    @DeleteMapping("/deleteUser")
//    public R<?> deleteUser(@RequestParam String ids) {
//        System.out.println("用户"+ids);
//        return R.success();
//    }

    @PostMapping("/checkUsername")
    public R<?> checkUsername(@RequestBody UsernameCheckDTO usernameCheckDTO) {
        String username = usernameCheckDTO.getUsername();
        boolean exists = userService.existsByUsername(username);
        return R.success(Collections.singletonMap("exists", exists));
    }

    // 修改后（后端 Controller）
//    @PostMapping("/checkUsername")
//    public R<?> checkUsername(@RequestParam Map<String, UsernameCheckDTO> params) {  // 接收查询参数
//        String username = params.get("params").getUsername();
//        boolean exists = userService.existsByUsername(username);
//        return R.success(Collections.singletonMap("exists", exists));
//    }
}
